<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use think\Db;
use think\db\Where;
use app\service\orderService;
class Parkinggate extends Common{
    public function gatelist(){
        //根据当前账号查询区域
        $com = (new Common())->pidTokenId();
        $whe=new Where;
        $where=new Where;
        $whe['community_id'] = array('in',$com['community_id']);
        //根据账号查询停车场
        $parkinglist = (new orderService())->parkingList($whe);
        $this->assign('parkinglist',$parkinglist);
        //查询停车场ID
        if(!empty($parkinglist)){
            foreach($parkinglist as $park){
                $parkingNo[] = $park['parkingNo'];
            }
            //将数组转换成字符串
            $parkingNo = implode(',',$parkingNo);
        }else{
            $parkingNo = null;
        }
        
        $gatelist =Db::name('parking_gatepay')->where('parkingNo','in',$parkingNo)->select();
        $number = Db::name('parking_gatepay')->where('parkingNo','in',$parkingNo)->count();
        $this->assign('number',$number);
        $this->assign('list',$gatelist);
        
       
       
        return $this->fetch('gate_list');
    }
    
    public function gateadd(){
        if(request()->isPost()){
            $data=request()->post();
            $parkinglotinfo=Db::table("parkinglotinfo")->where("parkingNo",$data["parkingNo"])->find();
            if(empty($parkinglotinfo)){
                return ;
            }
            $data["parkingName"]=$parkinglotinfo["parkingName"];
            $res=Db::table("parking_gatepay")->insert($data);
            if($res){
                return "添加成功";
            }else{
                return "添加失败";
            }
           
        }else{
            //根据当前账号查询区域
            $com = (new Common())->pidTokenId();
            $whe=new Where;
            $where=new Where;
            $whe['community_id'] = array('in',$com['community_id']);
            //根据账号查询停车场
            $parkinglist = (new orderService())->parkingList($whe);
            $this->assign('parkinglist',$parkinglist);
            return $this->fetch("gate_add");
        }
        
    }

    public function gateedit(){
        if(request()->isPost()){
            $data=request()->post();


            $res=Db::table("parking_gatepay")->update($data);
            if($res){
                return "配置成功";
            }else{
                return "配置失败";
            }

        }else{
            //根据当前账号查询区域
            $id=request()->get("id");
            $info=Db::table("parking_gatepay")->where("id",$id)->find();
            if(empty($info)){
                return ;
            }
            $this->assign('info',$info);
            return $this->fetch("gate_edit");
        }

    }
}